<?php
############## DIT STUK CODE ZORGT ERVOOR DAT JE ALS ADMIN INGELOGD MOET ZIJN OM DE PAGINA TE KUNNEN ZIEN ##############
    //Start de sessie
    session_start();

    //Load de benodigde bestanden
    require_once("includes/auth.php");
########################################################################################################################
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Bestellingen beheren</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="../css/pagination.css" rel="stylesheet" />
  <link rel="stylesheet" href="../css/reset.css" type="text/css" />
  <link rel="stylesheet" href="../css/960.css" type="text/css" />
  <link rel="stylesheet" href="../css/text.css" type="text/css" />
  <link type="text/css" href="../css/tablesorter.css" rel="stylesheet" />
  <link type="text/css" href="../css/besteltable.css" rel="stylesheet" />
  <link type="text/css" href="../css/pager.css" rel="stylesheet" />
  <link type="text/css" href="../css/sunny/jquery-ui-1.8.custom.css" rel="stylesheet" />
  <link type="text/css" href="../css/jquery.autocomplete.css" rel="stylesheet" />
  <link type="text/css" href="../css/style.css" rel="stylesheet" />
  <link type="text/css" href="../css/accmenu.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="../js/jquery-ui-1.8.custom.min.js">
</script>
<script type="text/javascript" src="../js/jquery.tablesorter.js">
</script>
<script type="text/javascript" src="../js/jquery.tablesorter.pager.js">
</script>
<script type="text/javascript" src="../js/jquery.autocomplete.min.js">
</script>
<script type="text/javascript" src="../js/menu.js">
</script>
<script type="text/javascript">
$(document).ready(function()
    {
        $("#facttable")
            .tablesorter()
            .tablesorterPager({container: $("#pager"), positionFixed: false});
    }
);
</script>
</head>

<body>
  <div class="container_12">
    <div id="header" class="grid_12">

    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>

    <div id="menu" class="grid_2">

        <?php
        include_once 'temp/menu_filler.php';
        ?>


    </div>

    <div id="adminarea" class="grid_10">
        <div id="tableholder">

                <?php

                // Database connection
                include 'includes/connect.php';

                setlocale(LC_ALL, 'nl_NL');

                $sql = "SELECT factuur.factuurid, factuur.klantid, UNIX_TIMESTAMP(factuur.datumtijd) as fdatum, klant.voornaam, klant.tussenvoegsel, klant.achternaam FROM factuur, klant WHERE factuur.klantid = klant.klantid";

                $facturen = mysql_query($sql);
                if (!$facturen) {
                  exit('<p>Error retrieving facturen from database!<br />'.
                      'Error: ' . mysql_error() . '</p>');
                }
                echo "<table class='besteltable' id='facttable' border='1' CELLPADDING=5>";
                echo "<thead><tr> <th>Factuur ID</th> <th>Klant ID</th> <th>Naam Klant</th> <th>Datum</th> <th>Totaal Bedrag</th> <th>Bewerken</th> <th>Route</th> <th>Factuur</th> </tr></thead>";
                while ($factuur = mysql_fetch_array($facturen)) {
                  $fid = $factuur['factuurid'];
				  $kid = $factuur['klantid'];
                  $factdatum = strftime("%d %B %Y", $factuur["fdatum"]);
                  $naam = $factuur['achternaam'].",&nbsp;".$factuur['voornaam']."&nbsp;".$factuur['tussenvoegsel'];

                  $bedragsql = "SELECT SUM(aantal * vkprijs) as totaalprijs FROM factuurregel WHERE factuurid=$fid";
                  $bedragen = mysql_query($bedragsql);
                  if (!$bedragen) {
                    exit('<p>Error retrieving facturen from database!<br />'.
                      'Error: ' . mysql_error() . '</p>');
                    }
                  $totaalbedrag = mysql_fetch_array($bedragen);
                  $bedrag = $totaalbedrag['totaalprijs'];
                  // Print out the contents of each row into a table
                echo "<tr><td><center>";
                echo $fid;
                echo "</center></td><td><center>";
                echo $kid;
                echo "</center></td><td><center>";
                echo $naam;
                echo "</center></td><td><center>";
                echo $factdatum;
                echo "</center></td><td><center>&euro;&nbsp;";
                echo number_format($bedrag,2);
                echo "</center></td><td><center>";
                echo "<a href='edit_fact.php?fid=$fid'>Bewerken</a>";
                echo "</center></td><td><center>";
		        echo "<a href='route.php?id=$kid' onclick=\"window.open('route.php?id=$kid','Route','width=630,height=768,scrollbars=yes,toolbar=yes'); return false\">Route</a>";
		        echo "</center></td><td><center>";
                echo "<a href='fact_pdf.php?id=$fid'>Download</a></center></td></tr>";
                }
                echo "</table>";
                echo "<br />";

                ?>
                <center><div id="pager" class="pager">
            	<form>
            		<img src="../css/icons/first.png" class="first"/>
            		<img src="../css/icons/prev.png" class="prev"/>
            		<input type="text" class="pagedisplay"/>
            		<img src="../css/icons/next.png" class="next"/>
            		<img src="../css/icons/last.png" class="last"/>
            		<select class="pagesize">
            			<option value="5">5</option>
            			<option selected="selected" value="10">10</option>
            		</select>
            	</form>
            </div></center>

        </div>
    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>
</div>
</body>
</html>